package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.WxAddressSaveBo;
import com.cskaoyan.bean.pojo.MallUser;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.WxAddressDetailVo;
import com.cskaoyan.bean.vo.WxAddressListVo;
import com.cskaoyan.bean.vo.WxRegionListVo;
import com.cskaoyan.service.RegionListService;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 小程序收货地址，获取对应的用户的全部，并查询地区地址：省、市、区
 * @Author: zoey-zhu
 * @Date: 14:55 2021/7/12
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

   /**
    * @Author: zoey-zhu
    * @description: 小程序收货地址，获取对应的用户的全部，并查询地区地址：省、市、区
    * @Date: 15:28 2021/7/12
    * @url: http://182.92.235.201:8083/wx/address/list
    */
    @GetMapping("/list")
    public BaseRespVo listByPid() {

//        int userId = 1;

        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();
        int userId = mallUser.getId();

        List<WxAddressListVo> wxAddressVoList = wxAddressService.list(userId);
        return BaseRespVo.ok(wxAddressVoList);
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，获取对应的用户的地址(通过地址id)
     * @Date: 17:08 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/detail?id=5
     */
    @GetMapping("/detail")
    public BaseRespVo detailById(Integer id) {

        WxAddressDetailVo wxAddressDetailVo = wxAddressService.detailById(id);
        return BaseRespVo.ok(wxAddressDetailVo);
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，对应的用户的地址，更新或者新增
     *              （根据id是否为0，为0insert，否则update），
     *              注意所以地址只能有一个默认地址：isDefault为true或者为1
     * @Date: 17:08 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/save
     */
    @PostMapping("/save")
    public BaseRespVo saveUpdateOrAdd(@RequestBody WxAddressSaveBo wxAddressSaveBo) {

//        int userId = 1;

        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();
        int userId = mallUser.getId();

        Integer id = wxAddressService.saveUpdateOrAdd(wxAddressSaveBo, userId);
        return BaseRespVo.ok(id);
    }

    /**
     * @Author: zoey-zhu
     * @description: 小程序收货地址，对应的用户的某个地址（根据地址id删除）
     * @Date: 18:59 2021/7/12
     * @url: http://182.92.235.201:8083/wx/address/delete
     */
    @PostMapping("/delete")
    public BaseRespVo deleteById(@RequestBody Map<String, Integer> map) {

        int delete = wxAddressService.deleteById(map.get("id"));
        return BaseRespVo.ok();
    }
}